package com.ccrfid.rmc6.service.sys;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.sys.DictEntity;
import com.ccrfid.rmc6.entity.sys.DictTypeEntity;
import com.ccrfid.rmc6.repository.sys.DictRepository;
import com.ccrfid.rmc6.repository.sys.DictTypeRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DictServiceImpl implements DictService {
    private static final Logger logger = LoggerFactory.getLogger(DictServiceImpl.class);
    @Autowired
    private DictTypeRepository dictTypeRepository;
    @Autowired
    private DictRepository dictRepository;

    @Override
    public List<DictTypeEntity> getDictTypeList() {
        return dictTypeRepository.getAllBy();
    }

    @Override
    public List<DictEntity> getDictList() {
        return dictRepository.getAllBy();
    }

    @Override
    public List<DictEntity> getDictByType(Integer typeId) {
        return dictRepository.getAllByTypeIdAndEnabled(typeId, 1);
    }

    @Override
    public DictEntity getDict(Integer id) {
        return dictRepository.getById(id);
    }

    @Override
    public DictEntity updateDict(Integer id, DictEntity entity) throws AppException {
        if (!dictRepository.existsById(id)) {
            throw new AppException(Response.Status.NOT_FOUND.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        if (!id.equals(entity.getId())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.different_args.getCode(), CustomErrors.different_args.getReason(), "");
        }
        return dictRepository.save(entity);
    }

    @Override
    public List<DictEntity> getDictAndSubDictListById(Integer id) {
        List<DictEntity> resultList = new ArrayList<>();
        DictEntity dictEntity = dictRepository.getById(id);
        if (dictEntity != null) {
            resultList.add(dictEntity);
            List<DictEntity> subList = dictRepository.getAllByPid(id);
            if (subList != null && !subList.isEmpty()) {
                resultList.addAll(subList);
            }
        }
        return resultList;
    }

    @Override
    public List<Integer> getDictAndSubDictIdListById(Integer id) {
        List<DictEntity> entityList = getDictAndSubDictListById(id);
        List<Integer> resultList = new ArrayList<>();
        if (entityList != null) {
            for (DictEntity dictEntity : entityList) {
                resultList.add(dictEntity.getId());
            }
        }
        return resultList;
    }

    @Override
    public List<DictEntity> getFocusedPrisonerTypes() {
        return dictRepository.getAllByTypeIdAndEnabled(15, 1);
    }

    @Override
    public List<DictEntity> getRoomManagerTypes() {
        return dictRepository.getAllByTypeIdAndEnabled(12, 1);
    }

    @Override
    public List<DictEntity> getPersonnelTypes() {
        return dictRepository.getAllByTypeIdAndEnabledAndPidIsNull(11, 1);
    }

    @Override
    public List<DictEntity> getPersonnelTypeWithSubs() {
        return dictRepository.getAllByTypeIdAndEnabled(11, 1);
    }

    @Override
    public List<DictEntity> getLocationTypes() {
        return dictRepository.getAllByTypeIdAndEnabled(31, 1);
    }

    @Override
    public List<DictEntity> getAlarmLevels() {
        return dictRepository.getAllByTypeIdAndEnabled(41, 1);
    }

    @Override
    public List<DictEntity> getAlarmTypes() {
        return dictRepository.getAllByTypeIdAndEnabled(42, 1);
    }

    @Override
    public List<DictEntity> getGuardTypes() {
        return dictRepository.getGuardTypes();
    }

    @Override
    public List<DictEntity> getAlarmLinkageModes() {
        return dictRepository.getAllByTypeIdAndEnabled(43, 1);
    }

    @Override
    public List<DictEntity> getVideoPlatformTypes() {
        return dictRepository.getAllByTypeIdAndEnabledAndPidIsNull(51, 1);
    }

    @Override
    public List<DictEntity> getVideoCameraTypes() {
        List<DictEntity> list = new ArrayList<>();
        list.addAll(dictRepository.getAllByTypeIdAndEnabledAndPidIsNull(51, 1));
        list.addAll(dictRepository.getAllByTypeIdAndEnabledAndPidIsNull(52, 1));
        return list;
    }

    @Override
    public List<DictEntity> getVideoVersions(Integer dictId) {
        return dictRepository.getAllByPid(dictId);
    }

    @Override
    public List<DictEntity> getVideoCameraShapes() {
        return dictRepository.getAllByTypeIdAndEnabledAndPidIsNull(53, 1);
    }

    @Override
    public List<DictEntity> getNodeTypes() {
        return dictRepository.getAllByTypeIdAndEnabledAndPidIsNull(71, 1);
    }
}
